package com.ghn.cocknovel.ui.activity.map

import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.TextUtils
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import com.example.basemodel.base.BaseHeadVMActivity
import com.example.basemodel.base.DefaultHeadStyle
import com.ghn.cocknovel.R
import com.ghn.cocknovel.constants.Constants
import com.ghn.cocknovel.databinding.ActivityMapCentenBinding
import com.ghn.cocknovel.ext.INVISIBLE
import com.ghn.cocknovel.ext.VISIBLE
import com.ghn.cocknovel.ui.activity.map.vm.MapViewModel
import com.ghn.cocknovel.ui.adapter.TabLayoutAdapter
import com.ghn.cocknovel.view.DialogMessage
import com.jeremyliao.liveeventbus.LiveEventBus
import com.tencent.map.fusionlocation.model.TencentGeoLocation
import com.tencent.navix.api.NavigatorZygote
import com.tencent.navix.api.location.GeoLocationObserver
import com.tencent.navix.api.location.LocationApi

class MapCentenActivity : BaseHeadVMActivity<MapViewModel, ActivityMapCentenBinding, DefaultHeadStyle>() {
    private val fragmentList = ArrayList<Fragment>()
    var titles = ArrayList<String>()
    protected var siteLatitude: Double = 0.0
    protected var siteLongitude: Double = 0.0


    var mTaskid = ""
    var mSiteId = ""
    var type = 1
    var mapJump = false//是否从地图跳转
    lateinit var locationApi: LocationApi
    override fun layoutId() = R.layout.activity_map_centen

    override fun initView(savedInstanceState: Bundle?) {
        mTaskid =
            if (intent.getStringExtra("taskId") == null) "" else intent.getStringExtra("taskId")
                .toString()
        mSiteId = if (intent.getStringExtra("siteId") == null) "" else intent.getStringExtra("siteId")
            .toString()
        mapJump = intent.getBooleanExtra("mapJump", false)
        // 通过 NavigatorZygote
        locationApi = NavigatorZygote.with(this).locationApi()
        locationApi.addLocationObserver(locationObserver, 1000)
        mHeadStyle.setTitleColor("订单列表", getColor(R.color.black))
        mHeadStyle.setTitleBackgroundColor(getColor(R.color.gray5))
        fragmentList.clear()
        fragmentList.add(MapPassengerFragment(siteLatitude, siteLongitude, mTaskid))
        fragmentList.add(DebusFragment(siteLatitude, siteLongitude, mTaskid, type))
        fragmentList.add(FulllistFragment(mTaskid))
        titles.add("上车乘客")
        titles.add("下车乘客")
        titles.add("全部订单")
        mDatabind.viewpage.adapter = TabLayoutAdapter(supportFragmentManager, fragmentList, titles)
        mDatabind.mainTab.setupWithViewPager(mDatabind.viewpage)
        mDatabind.viewpage.offscreenPageLimit = fragmentList.size

        mDatabind.btnNext.setOnClickListener {
            mViewModel.queryPassengerBySite.value?.also {
                when (it.siteState) {
                    1 -> {
                        showLoading()
                        mViewModel.transitArriveStartingPoint(mSiteId.toInt(), siteLatitude.toString(), siteLongitude.toString())
                    }

                    2 -> {
                        showLoading()
                        mViewModel.updateDriverNextSite(mSiteId, siteLatitude.toString(), siteLongitude.toString())
                    }
                }
            }
        }
        if (mapJump) {
            mDatabind.btnNext.VISIBLE()
            mViewModel.queryTransitOrder()
        }

//        LiveEventBus.get<String>(Constants.EventBus.DRIVER_TO_SITE).observe(this) {
//            //查询站点倒计时
//            mViewModel.queryPassengerBySite.value?.also {
//                mViewModel.getSiteCountdown(it.siteId)
//            }
//
//        }
    }

    fun queryTransitOrder() {
        mViewModel.queryTransitOrder()
    }

    override fun createObserver() {
        mViewModel.queryOverTimeLiveData.observe(this) {
//            Handler(Looper.getMainLooper()).postDelayed({
//                //判断当前Activity
//                if (isFinishing) return@postDelayed
//                DialogMessage(this).setContext("已等待${it / 1000}秒”").setMessageContentColor(Color.RED).show()
//            }, it)
        }
        mViewModel.queryPassengerBySite.observe(this) {
            when (it?.siteState) {
                1 -> {
                    mDatabind.btnNext.text = "到达" + it.siteName + " 站"
                }

                2 -> {
                    if (it.nextSiteName != null && !TextUtils.isEmpty(it.nextSiteName)) {
                        mDatabind.btnNext.text = "前往下一站:" + it.nextSiteName + " 站"
                    } else {
                        mDatabind.btnNext.text = "继续行程"
                    }
                }

                3 -> {
                    mDatabind.btnNext.text = "暂无行程"
                    mDatabind.btnNext.INVISIBLE()
                }
            }

            when (it?.clickState) {
                1 -> {
                    mDatabind.btnNext.isClickable = true
                    mDatabind.btnNext.isFocusable = true
                    mDatabind.btnNext.solid = getColor(R.color.blue2)
                }

                2 -> {
                    mDatabind.btnNext.isClickable = false
                    mDatabind.btnNext.isFocusable = false
                    mDatabind.btnNext.solid = getColor(R.color.gray8)
                }
            }

        }
        mViewModel.transitArriveStartingPoint.observe(this) {
            dismissLoading()
            LiveEventBus.get<String>("driverNextSite").post("1")
            finish()
        }
        mViewModel.updateDriverNextSite.observe(this) {
            dismissLoading()
            LiveEventBus.get<String>("driverNextSite").post("1")
            finish()
        }
        mViewModel.updateDriverNextSiteError.observe(this) {
            dismissLoading()
        }
        mViewModel.transitArriveStartingPointError.observe(this) {
            dismissLoading()
        }
    }

    val locationObserver: GeoLocationObserver = object : GeoLocationObserver() {
        override fun onGeoLocationChanged(tencentGeoLocation: TencentGeoLocation) {
            super.onGeoLocationChanged(tencentGeoLocation)
            siteLatitude = tencentGeoLocation.location.latitude
            siteLongitude = tencentGeoLocation.location.longitude
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        locationApi.removeLocationObserver(locationObserver)
    }
}
